using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using FeedBurner.Utility;
using System.Web;

namespace FeedBurner.Operations
{
    internal class GetFeed
    {
        public Feed Get(string username, string password, Int64 feedID, string uri)
        {
            StringBuilder Query = new StringBuilder
                (string.Format("?user={0}&password={1}", username, password));

            if (!string.IsNullOrEmpty(uri))
                Query.AppendFormat("&uri={0}",
                    HttpUtility.UrlEncode(uri, Encoding.ASCII));
            else if (feedID > 0)
                Query.AppendFormat("&id={0}", feedID);
            else
                throw new ArgumentException
                    ("One of feed ID or Uri must be provided.");

            string PureData = DataManager.Get(Urls.GetUrl() + Query.ToString());

            return GetSingleFeed(PureData);
        }

        private Feed GetSingleFeed(string Output)
        {
            Feed Result = new Feed();
            XmlDocument Xml = new XmlDocument();
            Xml.LoadXml(Output);

            if (Errors.CheckError(Xml))
            {
                XmlNode GlobalInfoNode =
                    Xml.SelectSingleNode("//rsp/feed");

                if (GlobalInfoNode != null)
                {
                    Result.ID = Convert.ToInt64
                          (GlobalInfoNode.Attributes["id"].InnerText);
                    Result.Title = GlobalInfoNode.Attributes["title"].InnerText;
                    Result.Uri = GlobalInfoNode.Attributes["uri"].InnerText;
                }

                XmlNode Url =
                    Xml.SelectSingleNode("//rsp/feed/source");

                if (Url != null)
                {
                    Result.Url = Url.Attributes["url"].InnerText;
                }

                XmlNodeList Services =
                    Xml.SelectNodes("//rsp/feed/services/service");

                if (Services != null)
                {
                    foreach (XmlNode XmlService in Services)
                    {
                        Service service = new Service(XmlService.Attributes["class"].InnerText);

                        XmlNodeList Parameters =
                            Xml.SelectNodes("//rsp/feed/services/service" +
                            "[@class = '" + service.Name + "']" +
                            "/param");

                        if (Parameters != null)
                        {
                            foreach (XmlNode Parameter in Parameters)
                            {
                                service.Parameters.Add
                                    (Parameter.Attributes["name"].InnerText,
                                    Parameter.InnerText);
                            }
                        }
                        Result.Services.Add(service);
                    }
                }

                return Result;
            }
            return null;
        }
    }
}
